package com.xnyzc.lhy.didi.tongcheng.entity;


import com.alibaba.fastjson.JSON;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.didi.tongcheng.penum.ETongChengErrorCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 接口返回样式
 */
@Data
@ApiModel(value = "返回实体", description = "返回实体")
public class TongChengRv<T> implements Serializable {

    /**
     * 错误码
     */
    @ApiModelProperty(value = "错误码，SUCCESS ： 成功 OTHER： 错误", example = "0")
    private int code;

    /**
     * 返回msg
     */
    @ApiModelProperty(value = "状态描述")
    private String message;

    /**
     * 返回数据
     */
    @ApiModelProperty(value = "返回数据")
    private T content;

    /**
     * 返回数据
     */
    @ApiModelProperty(value = "返回数据2")
    private T invoices;

    private TongChengRv() {
    }

    /**
     * 获取实例
     *
     * @param data
     * @return
     */
    public static <T> TongChengRv wrap(T data) {
        TongChengRv rv = new TongChengRv();

        rv.setCode(ETongChengErrorCode.success.getValue());
        rv.setMessage(ETongChengErrorCode.success.getDesc());
        rv.setContent(data);
        return rv;
    }

    /**
     * 获取实例
     *
     * @return
     */
    public static <T> TongChengRv wrap() {
        TongChengRv rv = new TongChengRv();

        rv.setCode(EErrorCode.success.getValue());
        rv.setMessage(EErrorCode.success.toChString());
        return rv;
    }

    /**
     * 获取实例
     *
     * @param data
     * @return
     */
    public static <T> TongChengRv error(ETongChengErrorCode data) {
        TongChengRv rv = new TongChengRv();

        rv.setCode(data.getValue());
        rv.setMessage(data.toChString());
        return rv;
    }

    /**
     * 获取实例
     *
     * @param msg
     * @param data
     * @return
     */
    public static <T> TongChengRv wrap(ETongChengErrorCode code, String msg, T data) {
        TongChengRv rv = new TongChengRv();

        rv.setCode(code.getValue());
        rv.setMessage(msg != null ? msg : code.getDesc());
        rv.setContent(data);
        return rv;
    }

    /**
     * 获取实例
     *
     * @param data
     * @return
     */
    public static <T> TongChengRv wrap(ETongChengErrorCode code, T data) {
        TongChengRv rv = new TongChengRv();

        rv.setCode(code.getValue());
        rv.setMessage(code.getDesc());
        rv.setContent(data);
        return rv;
    }

    /**
     * 获取实例
     *
     * @param data
     * @return
     */
    public static <T> TongChengRv wrap2(T data) {
        TongChengRv rv = new TongChengRv();
        rv.setCode(ETongChengErrorCode.success.getValue());
        rv.setMessage(ETongChengErrorCode.success.getDesc());
        rv.setInvoices(data);
        return rv;
    }

    public String toString() {
        return JSON.toJSONString(this);
    }
}
